Stata

您所在的位置:网站首页 stata tabulate Stata

Stata

2023-06-16 15:35| 来源: 网络整理| 查看: 265

How do I create dummy variables? Title   Creating dummy variables Author William Gould, StataCorp

A dummy variable is a variable that takes on the values 1 and 0; 1 means something is true (such as age < 25, sex is male, or in the category “very much”).

Dummy variables are also called indicator variables.

As we will see shortly, in most cases, if you use factor-variable notation, you do not need to create dummy variables.

In cases where factor variables are not the answer, you may use generate to create one dummy variable at a time and tabulate to create a set of dummies at once.

Using factor variables instead of generating dummy variables

I have a discrete variable, size, that takes on discrete values from 0 to 4

. tabulate size size | Freq. Percent Cum. ------------+----------------------------------- miniature | 19 19.00 19.00 small | 37 37.00 56.00 normal | 30 30.00 86.00 large | 12 12.00 98.00 huge | 2 2.00 100.00 ------------+----------------------------------- Total | 100 100.00

If I want a dummy for all levels of size except for a comparison group or base level, I do not need to create 4 dummies. Using [U] factor variables, I may type

. summarize i.size

or use an estimator

. regress y x i.size

If I want to use a dummy that is 1 if size is large (size==3) and 0 otherwise, I type

. regress y x 3.size

If I want to make the comparison group, or base level, of size be size==3 instead of the default size==0, I type

. regress y x ib3.size

You can also use factor-variable notation to refer to categorical variables, their interactions, or interactions between categorical and continuous variables.

For example, I can specify the interaction of each level of size (except the base level) and the continuous variable x by typing

. regress y x i.size#c.x

The c. instructs Stata that variable x is continuous.

In all the cases above, you did not need to create a variable.

Moreover, many of Stata's postestimation facilities, including in particular the margins command, are aware of factor variables and will handle them elegantly when making computations.

There are some instances where creating dummies might be worthwhile. We illustrate these below.

Using generate to create dummy variables

You could type

. generate young = 0 . replace young = 1 if age


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3